Task(TResult) Class

Task Parallel System.Threading

Represents an asynchronous operation that produces a result at some time in the future.

Namespace:  System.Threading.Tasks
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,  _
	ExternalThreading := True)> _
Public Class Task(Of TResult) _
	Inherits Task
C#
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class Task<TResult> : Task

Type Parameters

TResult
The type of the result produced by this Task<(Of <(TResult>)>).

Remarks

Task<(Of <(TResult>)>) instances may be created in a variety of ways. The most common approach is by using the task's Factory property to retrieve a TaskFactory<(Of <(TResult>)>) instance that can be used to create tasks for several purposes. For example, to create a Task<(Of <(TResult>)>) that runs a function, the factory's StartNew method may be used:

 Copy Code
            // C# 
            var t = Task<int>.Factory.StartNew(() => GenerateResult());
            - or -
            var t = Task.Factory.StartNew(() => GenerateResult());
            
            ' Visual Basic 
            Dim t = Task<int>.Factory.StartNew(Function() GenerateResult())
            - or -
            Dim t = Task.Factory.StartNew(Function() GenerateResult())
            

The Task<(Of <(TResult>)>) class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the StartNew method should be the preferred mechanism for creating and scheduling computational tasks, but for scenarios where creation and scheduling must be separated, the constructors may be used, and the task's Start method may then be used to schedule the task for execution at a later time.

All members of Task<(Of <(TResult>)>), except for Dispose, are thread-safe and may be used from multiple threads concurrently.

Inheritance Hierarchy

System..::.Object
  System.Threading.Tasks..::.Task
    System.Threading.Tasks..::.Task<(Of <(TResult>)>)

See Also